package com.zhangkeyou.map.mapweb.controller;

import com.zhangkeyou.map.mapweb.domain.entity.LoginUser;
import com.zhangkeyou.map.mapweb.domain.entity.MyFile;
import com.zhangkeyou.map.mapweb.domain.entity.Status;
import com.zhangkeyou.map.mapweb.service.FileService;
import com.zhangkeyou.map.mapweb.service.UserinfoService;
import com.zhangkeyou.map.mapweb.util.MyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/file")
public class FileController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);


    @Autowired
    private FileService fileService;
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping()
    public Status getFiles(@RequestParam("source") MyUtils.FileSource source){
        List<MyFile> files = fileService.selectFileListWithoutPath(source);
        return new Status(files);
    }

    @PostMapping("/upload2")
    public Status uploadFile2(MultipartFile file) throws IOException {

        String fileDownloadUrl = fileService.addFile(file, MyUtils.FileSource.FILE, null);
        return new Status<>(fileDownloadUrl);

    }
    @PostMapping("/upload2/article")
    public Status uploadFile2Article(MultipartFile file) throws IOException {
        String fileDownloadUrl = fileService.addFile(file, MyUtils.FileSource.ARTICLE, null);
        return new Status<>(fileDownloadUrl);
    }
    @PostMapping("/upload2/articlebywangeditor")
    public Status uploadFile2ArticleBywangEditor(MultipartFile file,
                                                 @RequestHeader("articleId") String articleId) throws IOException {
        String fileDownloadUrl = fileService.addFile(file, MyUtils.FileSource.ARTICLE,articleId);
        return new Status<>(fileDownloadUrl);
    }
    @GetMapping("/download2")
    public void fileDownload2(HttpServletResponse response,
                              @RequestParam("fileId") String fileId,
                              @RequestParam("source") MyUtils.FileSource source,
                              @RequestParam("userId") String userId) throws IOException {
        Status status = new Status();
        fileService.downloadFile(response,fileId,source,userId);

//        return status;
    }

    @GetMapping("/download")
    public void fileDownload(HttpServletResponse response,
                             @RequestParam("fileId") String fileId,
                             @RequestParam("source") MyUtils.FileSource source) throws IOException {
        fileService.downloadFile(response,fileId, source,null);
    }

    @DeleteMapping
    public Status deleteFiles(@RequestParam("fileId") String fileId, @RequestParam("source") MyUtils.FileSource source){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(Objects.isNull(loginUser))
            throw new RuntimeException("未登录");
        String userId = loginUser.getUserinfo().getId();
        return fileService.deleteFile(fileId,userId,source);
    }


}
